package com.gaofei.service.impl;

import java.util.List;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.gaofei.dao.SpecMapper;
import com.gaofei.domain.Spec;
import com.gaofei.domain.SpecOption;
import com.gaofei.service.SpecService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class SpecServiceImpl implements SpecService {

	@Autowired
	SpecMapper mapper;
	@Override
	public PageInfo<Spec> list(int pageNum, int pageSize) {
		
		PageHelper.startPage(pageNum, pageSize);
		
		return new PageInfo<>(mapper.list());
	}
	@Override
	public void del(String ids) {
		mapper.del(ids);
		
	}
	@Override
	public void add(Spec spec) {
		mapper.add(spec);//添加规格表
		List<SpecOption> optionList = spec.getOptionList();
		optionList.forEach((option)->{
			if(option!=null && option.getOptionName()!="" && option.getOptionName()!=null) {
				mapper.addSpecOption(option,spec.getId());//添加规格选项
			}
			
		});
	}
	@Override
	public Spec findSpecById(Integer id) {
		return mapper.findSpecById(id);
	}
	@Override
	public void update(Spec spec) {
		//1.根据specId修改spec
		mapper.update(spec);
		//2.根据specId 删除option选项
		mapper.delOptionBySpecId(spec.getId());
		//3.添加选项
		List<SpecOption> optionList = spec.getOptionList();
		optionList.forEach((option)->{
			if(option!=null && option.getOptionName()!="" && option.getOptionName()!=null) {
				mapper.addSpecOption(option,spec.getId());//添加规格选项
			}
			
		});
	}
	@Override
	public List<SpecOption> findSpecOptionBySpecId(Integer specId) {
		return mapper.findSpecOptionBySpecId(specId);
	}
	@Override
	public List<Spec> findSpecBySkuId(Integer id) {
		return mapper.findSpecBySkuId(id);	
	}

}
